package com.wizz.trade.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wizz.trade.information.dto.page.OtherCompanyPageDto;
import com.wizz.trade.information.dto.page.OurCompanyPageDto;
import com.wizz.trade.information.entity.Company;
import com.wizz.trade.information.mapper.CompanyMapper;
import com.wizz.trade.information.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-04-02 04:26:06
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public void addCompany(Company company) {
        companyMapper.insert(company);
    }

    @Override
    public void updateCompany(Company company) {
        companyMapper.updateById(company);
    }

    @Override
    public void deleteCompany(Integer id) {
        companyMapper.deleteById(id);
    }

    @Override
    public PageVo<Company> getPageOur(OurCompanyPageDto ourCompanyPageDto) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1);

//        queryWrapper.like(StringUtils.isNotBlank(ou.getFullname()),Company::getFullname, ou.getFullname());
//        queryWrapper.like(StringUtils.isNotBlank(ou.getShortname()),Company::getShortname,ou.getShortname());
//        queryWrapper.eq(!Objects.isNull(ou.getType()), Company::getType, ou.getType());
        IPage<Company> page = companyMapper.selectPage(PageUtil.initPage(ourCompanyPageDto), queryWrapper);
        return new PageVo<>(page);
    }

    @Override
    public List<Company> getAll() {
        return companyMapper.selectList(null);
    }

    @Override
    public PageVo<Company> getPageOthers(OtherCompanyPageDto otherCompanyPageDto) {
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        companyQueryWrapper.ne("type", 1);
        System.out.println(otherCompanyPageDto.getType());
        companyQueryWrapper.eq(StringUtils.isNotBlank(otherCompanyPageDto.getType() == null ? null : otherCompanyPageDto.getType().toString()), "type", otherCompanyPageDto.getType());
        companyQueryWrapper.eq(StringUtils.isNotBlank(otherCompanyPageDto.getFullname()), "fullname", otherCompanyPageDto.getFullname());
        companyQueryWrapper.eq(StringUtils.isNotBlank(otherCompanyPageDto.getShortname()), "shortname", otherCompanyPageDto.getShortname());

        IPage<Company> page = companyMapper.selectPage(PageUtil.initPage(otherCompanyPageDto), companyQueryWrapper);
        return new PageVo<>(page);
    }

    @Override
    public List<Map<Integer, String>> getLedgerOptions() {
        return companyMapper.getLedgerOptions();
    }

    @Override
    public List<Map<Integer, String>> getCompanyOptions() {
        return companyMapper.getCompanyOptions();
    }


}
